const app = getApp<IAppOption>() as any
import { requestGET } from '../../utils/service'
import { calculateAge } from '../../utils/util'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    reservationDate: '',
    reservationId: '',
    reservationNumber: '',
    physicalExaminationPackageTitle: '',
    pageData: {},
    foldIndex: -1,
    showDetailModal: false,
    detailModalData: {},
    iconImgObj: {
      蛋类: 'https://report.bayajk.com/baya_health/static/temporary/蛋类.png',
      调味品: 'https://report.bayajk.com/baya_health/static/temporary/调味品.png',
      调味料: 'https://report.bayajk.com/baya_health/static/temporary/调味品.png',
      坚果类: 'https://report.bayajk.com/baya_health/static/temporary/坚果类.png',
      粮谷: 'https://report.bayajk.com/baya_health/static/temporary/粮谷.png',
      粮谷类: 'https://report.bayajk.com/baya_health/static/temporary/粮谷类.png',
      肉类: 'https://report.bayajk.com/baya_health/static/temporary/肉类.png',
      其他类: 'https://report.bayajk.com/baya_health/static/temporary/其他类.png',
      乳类豆制品类: 'https://report.bayajk.com/baya_health/static/temporary/乳类豆制品类.png',
      乳类豆制品: 'https://report.bayajk.com/baya_health/static/temporary/乳类豆制品.png',
      蔬菜: 'https://report.bayajk.com/baya_health/static/temporary/蔬菜.png',
      水产类: 'https://report.bayajk.com/baya_health/static/temporary/水产类.png',
      水果: 'https://report.bayajk.com/baya_health/static/temporary/水果.png',
      饮料类: 'https://report.bayajk.com/baya_health/static/temporary/饮料类.png',
      油类: 'https://report.bayajk.com/baya_health/static/temporary/油类.png',
      晒太阳: 'https://report.bayajk.com/baya_health/static/temporary/其他类.png',
      鱼肝油: 'https://report.bayajk.com/baya_health/static/temporary/其他类.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let userInfo = wx.getStorageSync('userInfo')
    userInfo.age = calculateAge(userInfo.birthDay)
    this.setData({
      userInfo: userInfo,
      reservationId: options.reservationId,
      reservationDate: options.reservationDate,
      physicalExaminationPackageTitle: options.physicalExaminationPackageTitle,
      reservationNumber: options.reservationNumber
    })
    this.getReportDetails(options.reservationId)
  },
  //获取报告详情
  getReportDetails(id: any) {
    const params = {
      reservationId: id
    }
    requestGET("physicalExaminationRecordDetail/queryByPhysicalExaminationRecordId", params)
      .then((res: any) => {
        if (res.data.code == '200') {
          let data = res.data.data
          data['维生素'].forEach((item: any) => {
            item.standardDeviation = item.standardDeviation.toFixed(3)
            item.physicalExaminationPackageBasis.lowerBound = item.physicalExaminationPackageBasis.lowerBound.toFixed(3)
            item.physicalExaminationPackageBasis.upperBound = item.physicalExaminationPackageBasis.upperBound.toFixed(3)
          });
          data['微量元素'].forEach((item: any) => {
            item.standardDeviation = item.standardDeviation.toFixed(3)
            item.physicalExaminationPackageBasis.lowerBound = item.physicalExaminationPackageBasis.lowerBound.toFixed(3)
            item.physicalExaminationPackageBasis.upperBound = item.physicalExaminationPackageBasis.upperBound.toFixed(3)
          });
          data['食物'].forEach((item2: any) => {
            item2.foodList.forEach((item3: any) => {
              item3.standardDeviation = item3.standardDeviation.toFixed(3)
            });
          });
          data['食物'].forEach((item2: any) => {
            item2.foodList.sort((a:any,b:any) => a.standardDeviation - b.standardDeviation)
          });
          console.log(data)
          this.setData({
            pageData: data
          })
        } else {
          wx.showToast({
            title: '服务异常',
            icon: 'error',
            duration: 2000
          })
        }
      })
  },
  // 折叠
  clickTab(e: any) {
    let index = -1
    if (e.currentTarget.dataset.index != this.data.foldIndex) {
      index = e.currentTarget.dataset.index
    }
    this.setData({
      foldIndex: index
    })
  },
  seeMore(e: any) {
    console.log(e.currentTarget.dataset.item)
    let detailModalData = e.currentTarget.dataset.item
    let foods = detailModalData.physicalExaminationPackageBasis.foodSource.split(';')
    let newFoods = [] as any
    let lastFoods = [] as any
    foods.forEach((item: any) => {
      var reg = /[\t\r\f\n\s]*/g;
      item = item.replace(reg, '')
      newFoods.push(item)
    });
    newFoods = newFoods.filter(function (element: any) {
      return element !== undefined && element !== null && element !== "";
    });
    newFoods.forEach((item: any) => {
      const title = item.split(':')[0] || item
      const value = item.split(':')[1] || ''
      lastFoods.push({
        title: title,
        value: value,
      })
    });
    detailModalData.foodList = lastFoods
    this.setData({
      detailModalData: detailModalData,
      showDetailModal: true
    })
  },
  // 关闭弹窗
  closeModal() {
    this.setData({
      showDetailModal: false
    })
  },
})